package view.user;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

import controller.TableHandler;
import controller.ActionEvents.ActionHandler;
import javax.swing.JButton;

/**
 * Users statistical history displays a JTable of the transactions the user is affiliated to
 * @author Christian
 *
 */
public class Statistics extends JPanel {
	
	private ActionHandler actionHandler;
	private JTable table;
	private JScrollPane scrollPane;
	private JLabel lblStatistics;
	private DefaultTableModel model;
	public String[] columnHeaders = {"Transaction Type", "From ID", "From Account", "To ID", "To Account", "Transaction note", "Amount"};
	private JButton btnClearTransactions;
	private Color tableColors = Color.WHITE;
	private Color tableShadeColors = Color.lightGray;

	/** Create the panel.*/
	public Statistics(ActionHandler actionHandler) {
		
		
		setLayout(null);
		setPreferredSize(new Dimension(750, 372)); //Sets the preffered size, so the BorderLayout doesnt
		//'overrules' the panel.
		this.actionHandler = actionHandler;
		setBackground(Color.WHITE);
		
		// Statistics header
		lblStatistics = new JLabel("Statistics");
		lblStatistics .setFont(new Font("Euphemia UCAS", Font.PLAIN, 28));
		lblStatistics .setBounds(321, 6, 163, 39);
		add(lblStatistics); // JLabel is added to JPanel
		
		btnClearTransactions = new JButton("");
		btnClearTransactions.setBounds(528, 9, 163, 49);
		btnClearTransactions.addActionListener(actionHandler);
		btnClearTransactions.setActionCommand("clearTransactions");
		add(btnClearTransactions);
	}
	/**
	 * Method ads table to frame, when its called.
	 * Ensures that the JTable isnt added to the frame when the model equals null.
	 */
public void addTable()
{
	model = new DefaultTableModel ( actionHandler.getFrame().getModelClass().getStatisticsRows(), columnHeaders ) 
	{
		
		// An array withholds class datatypes
		Class[] columnTypes = new Class[] 
		{
			String.class, Integer.class, String.class, Integer.class, String.class, Integer.class, String.class //kolonne datatyper
		};
		
		// method returns an array of the column types
		public Class getColumnClass(int columnIndex) 
		{
			return columnTypes[columnIndex];
		}
		
		//Array stores whether a column can be edited by the user.
		boolean[] columnEditables = new boolean[] 
		{
			false, false, false, false, false, false //sets every field to non-editable
		};
		public boolean isCellEditable(int row, int column) 
		{
			return columnEditables[column];
		}
	};
	
	// An object of JTable is created
	table = new JTable(){
		public Component prepareRenderer(TableCellRenderer renderer,int Index_row, int Index_col) {
			Component comp = super.prepareRenderer(renderer, Index_row, Index_col);
			//even index, selected or not selected
			if (Index_row % 2 == 0 && !isCellSelected(Index_row, Index_col)) {
				comp.setBackground(tableShadeColors);
			} 
			else {
				comp.setBackground(tableColors);
			}
			return comp;
		}
		//Creates an instance of DefaultTableCellRenderer
		 DefaultTableCellRenderer renderCenter = new DefaultTableCellRenderer();

         {//initializer block
             renderCenter.setHorizontalAlignment(SwingConstants.CENTER);
         }

          @Override
       public TableCellRenderer getCellRenderer(int Index_row, int Index_col) {
              return renderCenter; // returns the object of DefaultTableCellRenderer

       }
		 
	};
	
	JTableHeader header = table.getTableHeader();
	header.setBackground(tableColors);

	// JTables TableModel is initialized with the 2D array and columnhead
	
	
	
	
	table.setModel(model);
	// The user can't re order the columnheader
	table.getTableHeader().setReorderingAllowed(false);
	
	//Sets the colors on JTable
	table.setBackground(tableColors);
	table.setSelectionBackground(tableColors);
	table.setGridColor(tableColors);
	table.setSelectionForeground(Color.BLACK);
	
	// Sets the preferred width on the column model
	table.getColumnModel().getColumn(0).setPreferredWidth(60);
	table.getColumnModel().getColumn(1).setPreferredWidth(20);
	table.getColumnModel().getColumn(2).setPreferredWidth(20);
	table.getColumnModel().getColumn(3).setPreferredWidth(20);
	table.getColumnModel().getColumn(4).setPreferredWidth(120);
	table.getColumnModel().getColumn(5).setPreferredWidth(120);
	table.getTableHeader().setResizingAllowed(false);
	table.setSize(1000,400);
	
	//JTable is added to JScrollPane, so the user can scroll up and down in case
	//The can't have the focus area on all rows at once.
	scrollPane = new JScrollPane(table);
	scrollPane.setBackground(tableColors);
	scrollPane.getViewport().setBackground(tableColors);
	scrollPane.setBorder(BorderFactory.createEmptyBorder());
	scrollPane.setForeground(tableColors);
	scrollPane.getVerticalScrollBar();
	scrollPane.setBounds(25, 60, 700, 400);
	add(scrollPane);
	
	//The text in the JTables column are set to be centered
	TableColumn column = table.getColumnModel().getColumn(5);  
    DefaultTableCellRenderer defaultTableCellRenderer = new DefaultTableCellRenderer();    
    defaultTableCellRenderer.setHorizontalAlignment(SwingConstants.CENTER);  
    defaultTableCellRenderer.setHorizontalTextPosition(SwingConstants.CENTER);
    column.setCellRenderer(defaultTableCellRenderer);  
}
	
		
	
	/**
	 * @return the model
	 */
	public DefaultTableModel getModel() {
		return model;
	}

	/**
	 * @param model the model to set
	 */
	public void setModel(DefaultTableModel model) {
		this.model = model;
	}

	/**
	 * @return the table
	 */
	public JTable getTable() {
		return table;
	}

	/**
	 * @param table the table to set
	 */
	public void setTable(JTable table) {
		this.table = table;
	}

	/**
	 * @return the scrollPane
	 */
	public JScrollPane getScrollPane() {
		return scrollPane;
	}
	/**
	 * @param tableColors the tableColors to set
	 */
	public void setTableColors(Color tableColors) {
		this.tableColors = tableColors;
	}
}
